package com.app.acitivty;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.app.activity.R;
import com.app.components.TopMenu;

public class MainActivity extends Activity {

	// private Button btnTicketScheduled;
	private int[] drawables = {R.drawable.bg_bottombtn_a1, R.drawable.bg_bottombtn_a1,
			R.drawable.bg_bottombtn_a1, R.drawable.bg_bottombtn_a1, R.drawable.bg_bottombtn_a1,
			R.drawable.bg_bottombtn_a1, R.drawable.bg_bottombtn_a1, R.drawable.bg_bottombtn_a1,
			R.drawable.bg_bottombtn_a1};
	
	private int[] desdrawables = { R.drawable.a1, R.drawable.a3,
			R.drawable.a2, R.drawable.a4, R.drawable.a5,
			R.drawable.a6, R.drawable.a7, R.drawable.a8,
			R.drawable.a9 };

	public void gotoPage(int page) {
		Intent intent = new Intent();
		switch (page) {
		case 1:
			intent.setClass(MainActivity.this, TicketScheduledActivity.class);
			break;
		case 2:
			intent.setClass(MainActivity.this, InnListSearchActivity.class);
			break;
		case 3:
			intent.setClass(MainActivity.this, ScenicSearchListActivity.class);
			break;
		case 4:
			intent.setClass(MainActivity.this, HotActivity.class);
			break;
		case 5:
			intent.setClass(MainActivity.this, BoughtListActivity.class);
			break;
		case 6:
			intent.setClass(MainActivity.this, FavoritesActivity.class);
			break;
		case 7:
			intent.setClass(MainActivity.this, YouHuiActivity.class);
			break;
		case 8:
			intent.setClass(MainActivity.this, WeatherActivity.class);
			break;
		case 9:
			intent.setClass(MainActivity.this, AboutActivity.class);
			break;
		}
		startActivity(intent);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		TopMenu topMenu = new TopMenu(this, "一网游遍丽江", true);
		topMenu.onCreate();
		GridView gridView = (GridView) findViewById(R.id.mainGridView);
		WindowManager manager = getWindowManager();
		Display display = manager.getDefaultDisplay();
		int width = display.getWidth()/4;
		gridView.setColumnWidth(width);
		gridView.setBackgroundColor(Color.WHITE);
		ArrayList<HashMap<String, Object>> meumlist = new ArrayList<HashMap<String, Object>>();
		int len = 9;
		for (int i = 0; i < len; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemImage", drawables[i]);
			map.put("ItemText", desdrawables[i]);
			meumlist.add(map);
		}
		MainAdapter adapter = new MainAdapter();
		gridView.setAdapter(adapter);
		gridView.setBackgroundResource(R.drawable.s_01);
//		HotelService service = new HotelService();
//		System.out.println(">>>>>>>>start service!!!");
//		service.execute(null);
//		SimpleAdapter spItem = new SimpleAdapter(this, meumlist,
//				R.layout.main_item, new String[] { "ItemImage", "ItemText" },
//				new int[] { R.id.bacimage, R.id.desimage });
//		gridView.setAdapter(spItem);
//		gridView.setOnItemClickListener(new OnItemClickListener() 
//		{
//			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//					long arg3) {
//				int index = arg2 + 1;
//				gotoPage(index);
//			}
//		});

	}
	
	class MainAdapter extends BaseAdapter
	{

		@Override
		public int getCount() {
			return drawables.length;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) 
		{
			MainHolder holder;
			if (convertView == null) {
				//parent.getWidth();
				convertView = LayoutInflater.from(getApplicationContext()).inflate
				(R.layout.main_item,null);
				//System.out.println("pos:" + position + ",convertView:"+convertView);
				holder = new MainHolder();
				holder.btn = (ImageButton) convertView.findViewById(R.id.bacimage);
				holder.imgView = (ImageView)convertView.findViewById(R.id.desimage);
				convertView.setTag(holder);
	
				convertView.setPadding(5, 25, 5, 25);
			}else
			{
				holder = (MainHolder) convertView.getTag();
			}
			holder.btn.setImageResource(drawables[position]);
			holder.btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					gotoPage(position+1);
				}
			});
			holder.imgView.setImageResource(desdrawables[position]);
			return convertView;
		}
		
	}
	
	class MainHolder
	{
		ImageButton btn;
		ImageView imgView;
	}
}